#!/bin/sh

source scripts/common.sh

####################################################################################################

IMPORT_VERSION=${DRISHTI_VERSION}
IMPORT_BASENAME="Import"
IMPORT_DIR="../../tools/import"

IMPORT_APP_BUNDLE_DIR=${BUILD_PRODUCTS_DIR}/${IMPORT_BASENAME}.app
IMPORT_APP_FRAMEWORK_DIR=${IMPORT_APP_BUNDLE_DIR}/Contents/Frameworks

####################################################################################################

pushd ${IMPORT_DIR} || exit 1

report "Removing old build ''${IMPORT_BASENAME}-${IMPORT_VERSION}'..."

rm -rf "${IMPORT_APP_BUNDLE_DIR}" || exit -1
rm -rf "${BUILD_PRODUCTS_DIR}/Drishti Import.app" || exit -1

####################################################################################################

report "Configuring ''${IMPORT_BASENAME}-${IMPORT_VERSION}'..."

${THIRD_PARTY_QT_DIR}/bin/qmake "CONFIG += x86 ppc x86_64" "QMAKE_CXXFLAGS += -fopenmp" "QMAKE_FLAGS += -fopenmp" T_VERSION="4.6.2" || exit 
if test $? -ne 0; then
        echo "configure failed"
        exit 1
fi

####################################################################################################

report "Building '${IMPORT_BASENAME}-${IMPORT_VERSION}'..."

make clean || exit 1
nice make -j8 || exit 1
nice make install || exit 1

####################################################################################################

popd
pushd ${BUILD_PRODUCTS_DIR} || exit -1

####################################################################################################

report "Installing dependencies..."

rm -rf $IMPORT_APP_FRAMEWORK_DIR || exit -1
mkdir -p $IMPORT_APP_FRAMEWORK_DIR || exit -1

install_dylib $IMPORT_BASENAME "Frameworks" "${THIRD_PARTY_QT_DIR}/lib/libQtCLucene.4.7.dylib" || exit 1
install_dylib $IMPORT_BASENAME "Frameworks" "/usr/local/lib/libjpeg.62.dylib" || exit -1
install_dylib $IMPORT_BASENAME "Frameworks" "/usr/local/lib/libpng12.0.dylib" || exit -1

for fw in ${QT_FRAMEWORK_LIST}; do
  install_qt_framework $IMPORT_BASENAME $fw
done

for dp in ${QT_FRAMEWORK_LIST}; do
for fw in ${QT_FRAMEWORK_LIST}; do
  localise_qt_framework $IMPORT_BASENAME $fw $dp
done

localise_dylib $IMPORT_BASENAME "Frameworks" $dp "${THIRD_PARTY_QT_DIR}/lib/libQtCLucene.4.7.dylib" || exit 1
localise_dylib $IMPORT_BASENAME "Frameworks" $dp "/usr/local/lib/libjpeg.62.dylib" || exit -1
localise_dylib $IMPORT_BASENAME "Frameworks" $dp "/usr/local/lib/libpng12.0.dylib" || exit -1

done

####################################################################################################

report "Installing QT Plugins..."
report ""

install_qt_plugins $IMPORT_BASENAME

report "Creating QT Config..."
report ""

create_qt_conf $IMPORT_BASENAME

####################################################################################################

popd

####################################################################################################

report "Installing resources..."
report ""

cp resources/DrishtiImportInfo.plist ${IMPORT_APP_BUNDLE_DIR}/Contents/Info.plist || exit -1 
mkdir -p ${IMPORT_APP_BUNDLE_DIR}/Contents/Resources 
cp  resources/DrishtiImport.icns ${IMPORT_APP_BUNDLE_DIR}/Contents/Resources || exit -1

mv ${IMPORT_APP_BUNDLE_DIR}/Contents/MacOS/${IMPORT_BASENAME} ${IMPORT_APP_BUNDLE_DIR}/Contents/MacOS/Drishti${IMPORT_BASENAME} || exit -1
mv "${IMPORT_APP_BUNDLE_DIR}" "${BUILD_PRODUCTS_DIR}/Drishti Import.app" || exit -1

report "Done!"

####################################################################################################


